{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ab364499",
   "metadata": {},
   "source": [
    "------\n",
    "* 2022-9-20 week 05\n",
    "* python课堂笔记\n",
    "* 记录人:梁颖\n",
    "------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1905a0dc",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "> 1. 有序的数据结构之列表\n",
    "\n",
    "> 2. 用户输入\n",
    "\n",
    "> 3. 关于格式化文本信息\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9fe2eaf0",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_01_name = \"Marry\"\n",
    "\n",
    "# 思考1：如果想一个变量存放一个班同学的名字，该怎么存？\n",
    "# 思考2：处理数据会不会比较麻烦？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bc4e81ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Marry,Alex,Jack,Bob\n"
     ]
    }
   ],
   "source": [
    "网一_name_list = \"Marry,Alex,Jack,Bob\"\n",
    "print(网一_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a2e70e1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Alex'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考2：处理数据会不会比较麻烦？\n",
    "网一_name_list[6:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "58b775c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考3：我想知道这个名单有多少个人名？\n",
    "len(网一_name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ca8e21e",
   "metadata": {},
   "source": [
    "## 1. 引入列表的概念\n",
    "* Lists might contain items of different types, but usually the items all have the same type\n",
    "> 注释：我们已经学习了 数值 和 字符，list可以放数值和字符，但是通常要么一起放数值，要么一起放字符串。\n",
    "\n",
    "> 总结一些通用的有序数据结构的方法：\n",
    ">> 1. 取值：通过索引值取出对应的内容\n",
    ">> 2. 切片：通过索引值区间取出对应的一些内容\n",
    ">> 3. in 可以查看成员对象是否存在\n",
    "\n",
    "> 列表的一些方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0996b572",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "54abb818",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Marry', 'Alex', 'Jack', 'Bob'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "网一_name_list = [\"Marry\",\"Alex\",\"Jack\",\"Bob\"]\n",
    "print(网一_name_list,type(网一_name_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c0702c9",
   "metadata": {},
   "source": [
    "### 1.1 列表的运算法则(Operation)\n",
    "* <font color = \"red\">列表的数据本身不做修改</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "cfe88449",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Marry'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出 网一_name_list 中第一位同学的名字\n",
    "网一_name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "63952c58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看 网一_name_list 有多少位同学\n",
    "len(网一_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "88598b09",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网一： Marry\n",
      "网一： Alex\n",
      "网一： Jack\n",
      "网一： Bob\n"
     ]
    }
   ],
   "source": [
    "for i in 网一_name_list:\n",
    "    print(\"网一：\",i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d3425731",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# in 可以查询数据是否存在\n",
    "\"Jack\" in 网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9e84934a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关于运算法则（in + 切片 取值 len(s) s.count)\n",
    "网一_name_list_转学生 = [\"Xiaoming\",\"Lihua\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d7d28c85",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', 'Bob', 'Xiaoming', 'Lihua']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list + 网一_name_list_转学生"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "0f31e04e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考：一个list 可不可以出现重复项？\n",
    "网一_age_list = [18,19,19,18,20,19]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "73bbcd0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 19, 18, 20, 19]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2bc59dd",
   "metadata": {},
   "source": [
    "### 2.2 列表的方法(Function)——对列表数据进行操作\n",
    "> 1. 增\n",
    "> 2. 删\n",
    "> 3. 改\n",
    "> 4. 查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38a413dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "### append 增加元素到末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a77408db",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list = \"Marry,Alex,Jack,Bob\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ba48f7c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Marry,Alex,Jack,Bob'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ace04993",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'str' object has no attribute 'append'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Input \u001b[0;32mIn [11]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43m网一_name_list\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mappend\u001b[49m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m张志豪\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'str' object has no attribute 'append'"
     ]
    }
   ],
   "source": [
    "网一_name_list.append(\"张志豪\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "737b0302",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Marry,Alex,Jack,Bob'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "37d64aaf",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'str' object has no attribute 'append'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Input \u001b[0;32mIn [13]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43m网一_name_list\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mappend\u001b[49m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m王一鸣\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'str' object has no attribute 'append'"
     ]
    }
   ],
   "source": [
    "网一_name_list.append(\"王一鸣\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3883ede2",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name '网一_name_list' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[0;32mIn [5]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43m网一_name_list\u001b[49m\n",
      "\u001b[0;31mNameError\u001b[0m: name '网一_name_list' is not defined"
     ]
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e50e87b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# insert 增加元素到某一位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a9ad7358",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name '网一_name_list' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[0;32mIn [3]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m help(\u001b[43m网一_name_list\u001b[49m\u001b[38;5;241m.\u001b[39minsert)\n",
      "\u001b[0;31mNameError\u001b[0m: name '网一_name_list' is not defined"
     ]
    }
   ],
   "source": [
    "help(网一_name_list.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "05721327",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list.insert(1,\"李华\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "d9aefc5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Alex', 'Jack', 'Bob', '张志豪', '王一鸣']"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03ffc031",
   "metadata": {},
   "outputs": [],
   "source": [
    "# remove "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "ad03772d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网一_name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "f0774db5",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list.remove(\"Alex\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "a9fadc17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Jack', 'Bob', '张志豪', '王一鸣']"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "843a8241",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Jack', 'Bob', '张志豪', '王一鸣']"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# remove 经常和 in 一起使用，先检查成员变量是否存在，然后再remove\n",
    "if \"Alex\" in 网一_name_list:\n",
    "    网一_name_list.remove(\"Alex\")\n",
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "c6d0a47f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "282242ff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网一_name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "27bed600",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Jack', 'Bob', '张志豪', '王一鸣']"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "02e81b12",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'王一鸣'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "5f197842",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Jack', 'Bob', '张志豪']"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "363d9b09",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jack'"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list.pop(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "2391efc4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Bob', '张志豪']"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "71c1998e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践：请移除19的元素\n",
    "age_list = [18,19,18,19,17,20,20,18,19,16,17,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "9d3572c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list.count(19)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "250f4214",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 19, 17, 20, 20, 18, 19, 16, 17, 20]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "50142ae4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路1: remove(value)，先决条件，知道值出现的次数\n",
    "删除的数量 = age_list.count(19)\n",
    "for i in range(删除的数量):\n",
    "    if 19 in age_list:\n",
    "        age_list.remove(19)\n",
    "    age_list\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "b58e39bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list = [18,19,18,19,17,20,20,18,19,16,17,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "2e8ad926",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 19, 17, 20, 20, 18, 19, 16, 17, 20]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "883293c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 19\n",
      "2 19\n",
      "6 19\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路2: pop(index),先决条件，知道值出现的位置\n",
    "for i,v in enumerate(age_list):\n",
    "    if v == 19:\n",
    "        print(i,v)\n",
    "        age_list.pop(i)\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2eedcf5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 课后练习：\n",
    "# 1. 课表P68和P81\n",
    "# 2. 剩下的 reserve，extend，clear，copy，sort回去之后尝试练习\n",
    "# 3. 总结列表方法，用表格的方式写在markdown上"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
